/**
 * @author DELL
 * @createDate 2016/11/15 15:11
 * @copyright 广州市生物制品有限公司
 * @description 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.lzw.web.api;

import com.lzw.command.CreditAccountCommand;
import com.lzw.command.DebitAccountCommand;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 类功能描述: TODO
 *
 * @author 12360
 * @version 1.0
 * @createDate Nov 19, 2015 11:16:44 AM
 */
@Controller
public class IndexController {

    @Autowired
    private CommandGateway commandGateway;

    @RequestMapping("/")
    public String index(Model model) {
        model.addAttribute("name", "dadepo");
        return "index";
    }

    @RequestMapping("/debit")
    @Transactional
    @ResponseBody
    public void doDebit(@RequestParam("acc") String accountNumber,
                        @RequestParam("amount") double amount) {
        DebitAccountCommand debitAccountCommand =
                new DebitAccountCommand(accountNumber, amount);

        commandGateway.send(debitAccountCommand);
    }

    @RequestMapping("/credit")
    @Transactional
    @ResponseBody
    public void doCredit(@RequestParam("acc") String accountNumber,
                         @RequestParam("amount") double amount) {
        CreditAccountCommand creditAccountCommand =
                new CreditAccountCommand(accountNumber, amount);
        commandGateway.send(creditAccountCommand);
    }

}
